package com.dsg.service.apply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.security.SystemAuthorizingUser;
import com.dsg.common.util.BeanKit;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.DateUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.apply.DsgApplyBillMapper;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.apply.DsgApplyPay;
import com.dsg.entity.dto.SupApproveDto;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.apply.*;
import com.dsg.util.Constant;
import com.dsg.workflow.WorkflowEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业务申请单 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-10
 */
@Service
public class DsgApplyBillServiceImpl extends DsgServiceImpl<DsgApplyBillMapper, DsgApplyBill> implements IDsgApplyBillService {
    @Autowired
    private IDsgApplyPayService payService;
    @Autowired
    private IDsgApplyCarService carService;
    @Autowired
    private IDsgApplyCusService cusService;
    @Autowired
    private WorkflowEngine engine;

    @Override
    public Page<DsgApplyBill> selectSalesManagerList(Page<DsgApplyBill> page, DsgApplyBill entity) {
        return baseMapper.selectSalesManagerList(page, entity);
    }

    @Override
    public Page<DsgApplyBill> selectSalesWaitingList(Page<DsgApplyBill> page, DsgApplyBill entity) {
        return baseMapper.selectSalesWaitingList(page, entity);
    }

    @Override
    public DsgApplyBill getBillByCard(String cardId) {
        return baseMapper.getBillByCard(cardId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String genBillAndGetAppNo(String cusNo) throws Exception {
        SystemAuthorizingUser user = SingletonLoginUtils.getSystemUser();
        DsgApplyBill bill = baseMapper.getMainBillByCusNo(cusNo, user.getOrgCode());
        bill.setOid(UUIDGenerator.getUUID());
        String appNo = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        bill.setAppNo(appNo);
        bill.setApplyTime(new Date());
        bill.setPayType(Constant.PAY_TYPE_ATP);
        bill.setAppStatus(DsgApplyBill.APP_STATUS_DRAFT);
        bill.setAppType(DsgApplyBill.APP_TYPE_20);
        bill.setSaleTeam(null);
        bill.setSalerNo(null);
        bill.setSalerRemarks(null);
        BeanKit.resetCommonField(bill);
        save(bill);
        engine.begin(bill);
        return appNo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public DsgApplyBill genAddPaymentBill(DsgApplyPay pay) throws Exception {
        SystemAuthorizingUser user = SingletonLoginUtils.getSystemUser();
        DsgApplyBill bill = baseMapper.getMainBillByCusNo(pay.getCusNo(), user.getOrgCode());
        bill.setOid(UUIDGenerator.getUUID());
        bill.setAppNo(pay.getAppNo());
        bill.setApplyTime(new Date());
        bill.setPayType(Constant.PAY_TYPE_ATP);
        bill.setAppStatus(DsgApplyBill.APP_STATUS_DRAFT);
        bill.setAppType(DsgApplyBill.APP_TYPE_30);
        bill.setSaleTeam(null);
        bill.setSalerNo(null);
        bill.setSalerRemarks(null);
        BeanKit.resetCommonField(bill);
        return bill;
    }

    @Override
    public Page<SupApproveDto> selectSupApproveList(Page<SupApproveDto> page, Integer appType, String approveStatus, String entCode) {
        return baseMapper.selectSupApproveList(page, appType, approveStatus, entCode, SingletonLoginUtils.getSystemUserId());
    }

    @Override
    public List<DsgApplyBill> selectApproveList() {
        return baseMapper.selectApproveList(SingletonLoginUtils.getSystemUserId());
    }

    public Page<DsgApplyBill> csWaitingList(Page<DsgApplyBill> page, DsgApplyBill entity) {
        entity.setUserId(SingletonLoginUtils.getSystemUserId());
        return baseMapper.csWaitingList(page, entity);
    }

    public boolean carFileComplete(String carOid) {
        return StringUtil.isNotEmpty(baseMapper.carFileComplete(carOid));
    }

    public boolean otherFileComplete(String appNo) {
        return StringUtil.isNotEmpty(baseMapper.otherFileComplete(appNo));
    }

    public Page<DsgApplyBill> selectUncheckedBill(Page<DsgApplyBill> page, DsgApplyBill bill, boolean isChecked) {
        bill.setUserId(SingletonLoginUtils.getSystemUserId());
        return baseMapper.selectUncheckedBill(page, bill, isChecked);
    }

    public boolean internalAudit(DsgApplyBill bill) throws Exception {
        boolean payAudit = payService.payAuditAll(bill);
        boolean carAudit = carService.carAuditAll(bill);
        boolean cusAudit = cusService.audit(bill);
        return payAudit && carAudit && cusAudit;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelExpiredReplacement(List<String> asList) {
        if (CollectionUtil.isNotEmpty(asList)) {
            DsgApplyBill bill = new DsgApplyBill();
            bill.setAppStatus(DsgApplyBill.APP_STATUS_2);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("OID", asList);
            if (baseMapper.update(bill, queryWrapper) > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
